<?php
include_once(ROOT_PATH . 'includes/lib_clips.php');

/* 没有验证码时，用时间来限制机器人发帖或恶意发评论 */
if (!isset($_SESSION['send_time'])) {
    $_SESSION['send_time'] = 0;
}

$cur_time = gmtime();
if (($cur_time - $_SESSION['send_time']) < 30) // 小于30秒禁止发评论
{
    abort($_LANG['cmt_spam_warning']);
}
$user_name = '';
if (empty($_POST['anonymous']) && !empty($_SESSION['user_name'])) {
    $user_name = $_SESSION['user_name'];
} elseif (!empty($_POST['anonymous']) && !isset($_POST['user_name'])) {
    $user_name = $_LANG['anonymous'];
} elseif (empty($_POST['user_name'])) {
    $user_name = $_LANG['anonymous'];
} else {
    $user_name = htmlspecialchars(trim($_POST['user_name']));
}

$user_id = !empty($_SESSION['user_id']) ? $_SESSION['user_id'] : 0;
$message = array(
    'user_id' => $user_id,
    'user_name' => $user_name,
    'user_email' => isset($_POST['user_email']) ? htmlspecialchars(trim($_POST['user_email'])) : '',
    'msg_type' => isset($_POST['msg_type']) ? intval($_POST['msg_type']) : 0,
    'msg_title' => isset($_POST['msg_title']) ? trim($_POST['msg_title']) : '意见',
    'msg_content' => isset($_POST['msg_content']) ? trim($_POST['msg_content']) : '',
    'order_id' => 0,
    'msg_area' => 1,
    'upload' => array()
);

if (add_message($message)) {
    if (intval($_CFG['captcha']) & CAPTCHA_MESSAGE) {
        unset($_SESSION[$validator->session_word]);
    } else {
        $_SESSION['send_time'] = $cur_time;
    }
    $msg_info = $_CFG['message_check'] ? $_LANG['message_submit_wait'] : $_LANG['message_submit_done'];
    return [];
} else {
    abort($_LANG['message_list_lnk']);
}
